extends "res://BaseEnemy.gd"

# 森林守护者Boss特有属性
@export var phase = 1  # 当前阶段
@export var phase_health_threshold = 0.5  # 进入第二阶段的生命值阈值
@export var vine_whip_cooldown = 5.0  # 藤蔓抽打冷却时间
@export var vine_whip_damage = 15  # 藤蔓抽打伤害
@export var nature_fury_cooldown = 15.0  # 自然之怒冷却时间
@export var nature_fury_duration = 5.0  # 自然之怒持续时间
@export var summon_cooldown = 10.0  # 召唤冷却时间
@export var healing_pulse_cooldown = 20.0  # 治疗脉冲冷却时间
@export var healing_amount = 50  # 治疗量

var vine_whip_timer = 0.0
var nature_fury_timer = 0.0
var summon_timer = 0.0
var healing_pulse_timer = 0.0
var can_vine_whip = true
var can_nature_fury = true
var can_summon = true
var can_healing_pulse = true
var is_nature_fury_active = false
var nature_fury_active_timer = 0.0

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置森林守护者Boss特有属性
	max_health = 600
	current_health = max_health
	movement_speed = 60.0
	damage = 12
	experience_value = 120
	is_boss = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
		health_bar.visible = true  # Boss始终显示生命值条
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.5, 1.5, 0.5)  # Boss颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 检查是否需要进入第二阶段
	if phase == 1 and current_health <= max_health * phase_health_threshold:
		enter_phase_two()
	
	# 处理藤蔓抽打冷却
	if not can_vine_whip:
		vine_whip_timer += delta
		if vine_whip_timer >= vine_whip_cooldown:
			can_vine_whip = true
			vine_whip_timer = 0.0
	
	# 处理自然之怒冷却
	if not can_nature_fury:
		nature_fury_timer += delta
		if nature_fury_timer >= nature_fury_cooldown:
			can_nature_fury = true
			nature_fury_timer = 0.0
	
	# 处理召唤冷却
	if not can_summon:
		summon_timer += delta
		if summon_timer >= summon_cooldown:
			can_summon = true
			summon_timer = 0.0
	
	# 处理治疗脉冲冷却
	if not can_healing_pulse:
		healing_pulse_timer += delta
		if healing_pulse_timer >= healing_pulse_cooldown:
			can_healing_pulse = true
			healing_pulse_timer = 0.0
	
	# 处理自然之怒效果
	if is_nature_fury_active:
		nature_fury_active_timer += delta
		
		# 每秒对玩家造成伤害
		if target_player and is_instance_valid(target_player) and int(nature_fury_active_timer) > int(nature_fury_active_timer - delta):
			var nearby_enemies = get_tree().get_nodes_in_group("enemies")
			for enemy in nearby_enemies:
				if enemy != self and is_instance_valid(enemy) and enemy.has_method("heal"):
					var distance = global_position.distance_to(enemy.global_position)
					if distance <= 200:
						enemy.heal(5)  # 每秒治疗5点生命值
			
			# 自身也恢复生命值
			heal(5)
		
		# 自然之怒结束
		if nature_fury_active_timer >= nature_fury_duration:
			is_nature_fury_active = false
	
	# 根据阶段和冷却状态使用技能
	if phase == 2:
		# 第二阶段更积极地使用技能
		if can_vine_whip and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 150 and randf() < 0.03:  # 每帧3%几率
				vine_whip_attack()
		
		if can_nature_fury and randf() < 0.01:  # 每帧1%几率
			activate_nature_fury()
		
		if can_summon and randf() < 0.02:  # 每帧2%几率
			summon_minions()
		
		if can_healing_pulse and current_health < max_health * 0.7 and randf() < 0.02:  # 生命值低于70%时每帧2%几率
			healing_pulse()
	else:
		# 第一阶段较少使用技能
		if can_vine_whip and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 150 and randf() < 0.02:  # 每帧2%几率
				vine_whip_attack()
		
		if can_nature_fury and randf() < 0.005:  # 每帧0.5%几率
			activate_nature_fury()
		
		if can_summon and randf() < 0.01:  # 每帧1%几率
			summon_minions()
		
		if can_healing_pulse and current_health < max_health * 0.5 and randf() < 0.01:  # 生命值低于50%时每帧1%几率
			healing_pulse()

func enter_phase_two():
	phase = 2
	
	if animation_player and animation_player.has_animation("transform"):
		animation_player.play("transform")
	
	# 增强属性
	movement_speed *= 1.2
	damage *= 1.3
	
	# 视觉效果
	if sprite:
		sprite.modulate = Color(0.3, 1.8, 0.3)  # 更亮的颜色
		sprite.scale *= 1.2  # 更大的尺寸
	
	# 立即使用自然之怒
	activate_nature_fury()
	
	print("森林守护者进入第二阶段!")

func vine_whip_attack():
	if animation_player and animation_player.has_animation("vine_whip"):
		animation_player.play("vine_whip")
	
	# 创建藤蔓效果
	var vine = preload("res://VineWhipEffect.tscn").instantiate()
	get_tree().get_root().add_child(vine)
	
	# 设置藤蔓属性
	if target_player and is_instance_valid(target_player):
		var direction = (target_player.global_position - global_position).normalized()
		vine.global_position = global_position
		vine.direction = direction
		vine.damage = vine_whip_damage
		vine.source = self
	
	# 重置藤蔓抽打冷却
	can_vine_whip = false
	vine_whip_timer = 0.0
	
	print("森林守护者使用藤蔓抽打!")

func activate_nature_fury():
	if animation_player and animation_player.has_animation("nature_fury"):
		animation_player.play("nature_fury")
	
	# 设置自然之怒状态
	is_nature_fury_active = true
	nature_fury_active_timer = 0.0
	can_nature_fury = false
	nature_fury_timer = 0.0
	
	# 创建自然之怒效果
	var nature_fury = preload("res://NatureFuryEffect.tscn").instantiate()
	get_tree().get_root().add_child(nature_fury)
	nature_fury.global_position = global_position
	nature_fury.duration = nature_fury_duration
	
	print("森林守护者释放自然之怒!")

func summon_minions():
	if animation_player and animation_player.has_animation("summon"):
		animation_player.play("summon")
	
	# 召唤随从
	var count = 2 if phase == 1 else 3
	for i in range(count):
		var minion_type = randi() % 3
		var minion_scene = ""
		
		match minion_type:
			0: minion_scene = "res://ForestGoblin.tscn"
			1: minion_scene = "res://ForestSpider.tscn"
			2: minion_scene = "res://ForestWolf.tscn"
		
		var minion = load(minion_scene).instantiate()
		get_tree().get_root().add_child(minion)
		
		# 设置随从位置（在Boss周围）
		var angle = TAU * i / count
		var offset = Vector2(cos(angle), sin(angle)) * 50
		minion.global_position = global_position + offset
	
	# 重置召唤冷却
	can_summon = false
	summon_timer = 0.0
	
	print("森林守护者召唤了", count, "个随从!")

func healing_pulse():
	if animation_player and animation_player.has_animation("healing_pulse"):
		animation_player.play("healing_pulse")
	
	# 创建治疗脉冲效果
	var pulse = preload("res://HealingPulseEffect.tscn").instantiate()
	get_tree().get_root().add_child(pulse)
	pulse.global_position = global_position
	
	# 治疗自身
	heal(healing_amount)
	
	# 重置治疗脉冲冷却
	can_healing_pulse = false
	healing_pulse_timer = 0.0
	
	print("森林守护者使用治疗脉冲!")

# 添加治疗方法
func heal(amount):
	if current_health < max_health:
		current_health = min(current_health + amount, max_health)
		
		# 更新生命值条
		if health_bar:
			health_bar.value = current_health
		
		# 创建治疗效果
		var heal_effect = preload("res://HealEffect.tscn").instantiate()
		get_tree().get_root().add_child(heal_effect)
		heal_effect.global_position = global_position

# 重写攻击方法
func attack_player():
	if phase == 2 and randf() < 0.4:  # 第二阶段40%几率使用藤蔓抽打
		vine_whip_attack()
	else:
		# 普通攻击
		super.attack_player()

# 重写受伤方法，添加阶段转换逻辑
func take_damage(amount):
	super.take_damage(amount)
	
	# 检查是否需要进入第二阶段
	if phase == 1 and current_health <= max_health * phase_health_threshold:
		enter_phase_two()
